package com.lj.common.utils.qywx.builder;

import com.lj.common.enums.ICommonEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

/**
 * @author luojing
 * @date 2025/3/22
 */
public class TemplateCardBuilder extends AbstractMsgBuilder {


    private TemplateCardBuilder() {
    }

    /**
     * 构建文本通知模板卡片
     *
     * @param mainTitleBuilder  模版卡片的主要内容，包括一级标题和标题辅助信息
     * @param cardActionBuilder 整体卡片的点击跳转事件，text_notice模版卡片中该字段为必填项
     */
    public static TemplateCardBuilder builderTextNotice(MainTitleBuilder mainTitleBuilder,
                                                        CardActionBuilder cardActionBuilder) {
        TemplateCardBuilder templateCardBuilder = new TemplateCardBuilder();
        templateCardBuilder.builder.put("card_type", CardType.TextNotice.value)
                .put("main_title", mainTitleBuilder.build())
                .put("card_action", cardActionBuilder.build());
        return templateCardBuilder;
    }

    /**
     * 构建图文展示模板卡片
     *
     * @param mainTitleBuilder  模版卡片的主要内容，包括一级标题和标题辅助信息
     * @param cardImageBuilder  图片样式
     * @param cardActionBuilder 整体卡片的点击跳转事件，news_notice模版卡片中该字段为必填项
     */
    public static TemplateCardBuilder builderNewsNotice(MainTitleBuilder mainTitleBuilder,
                                                        CardImageBuilder cardImageBuilder,
                                                        CardActionBuilder cardActionBuilder) {
        TemplateCardBuilder templateCardBuilder = new TemplateCardBuilder();
        templateCardBuilder.builder.put("card_type", CardType.NewsNotice.value)
                .put("main_title", mainTitleBuilder.build())
                .put("card_image", cardImageBuilder.build())
                .put("card_action", cardActionBuilder.build());
        return templateCardBuilder;
    }

    /**
     * @param imageTextAreaBuilder 左图右文样式
     */
    public TemplateCardBuilder imageTextArea(ImageTextAreaBuilder imageTextAreaBuilder) {
        builder.put("image_text_area", imageTextAreaBuilder.build());
        return this;
    }

    /**
     * @param verticalContentBuilderList 卡片二级垂直内容，该字段可为空数组，但有数据的话需确认对应字段是否必填，列表长度不超过4
     */
    public TemplateCardBuilder verticalContentList(List<VerticalContentBuilder> verticalContentBuilderList) {
        builder.put("vertical_content_list", verticalContentBuilderList.stream().map(VerticalContentBuilder::build).toList());
        return this;
    }

    /**
     * @param sourceBuilder 卡片来源样式信息，不需要来源样式可不填写
     */
    public TemplateCardBuilder source(SourceBuilder sourceBuilder) {
        builder.put("source", sourceBuilder.build());
        return this;
    }

    /**
     * @param emphasisContentBuilder 关键数据样式
     */
    public TemplateCardBuilder emphasisContent(EmphasisContentBuilder emphasisContentBuilder) {
        builder.put("emphasis_content", emphasisContentBuilder.build());
        return this;
    }

    /**
     * @param quoteAreaBuilder 引用文献样式，建议不与关键数据共用
     */
    public TemplateCardBuilder quoteArea(QuoteAreaBuilder quoteAreaBuilder) {
        builder.put("quote_area", quoteAreaBuilder.build());
        return this;
    }

    /**
     * @param subTitleText 二级普通文本，建议不超过112个字。模版卡片主要内容的一级标题main_title.title和二级普通文本sub_title_text必须有一项填写
     */
    public TemplateCardBuilder subTitleText(String subTitleText) {
        builder.put("sub_title_text", subTitleText);
        return this;
    }

    /**
     * @param horizontalContentBuilderList 二级标题+文本列表，该字段可为空数组，但有数据的话需确认对应字段是否必填，列表长度不超过6
     */
    public TemplateCardBuilder horizontalContentList(List<HorizontalContentBuilder> horizontalContentBuilderList) {
        builder.put("horizontal_content_list", horizontalContentBuilderList.stream()
                .map(HorizontalContentBuilder::build).toList());
        return this;
    }

    /**
     * @param jumpBuilderList 跳转指引样式的列表，该字段可为空数组，但有数据的话需确认对应字段是否必填，列表长度不超过3
     */
    public TemplateCardBuilder jumpList(List<JumpBuilder> jumpBuilderList) {
        builder.put("jump_list", jumpBuilderList.stream().map(JumpBuilder::build).toList());
        return this;
    }

    @AllArgsConstructor
    @Getter
    public enum CardType implements ICommonEnum<String> {
        TextNotice("text_notice", "文本通知模板卡片"),
        NewsNotice("news_notice", "图文展示模板卡片");
        private final String value;
        private final String desc;
    }

    public static class MainTitleBuilder extends AbstractMsgBuilder {
        private MainTitleBuilder() {
        }

        public static MainTitleBuilder builder() {
            return new MainTitleBuilder();
        }

        /**
         * @param title 一级标题，建议不超过26个字。模版卡片主要内容的一级标题main_title.title和二级普通文本sub_title_text必须有一项填写
         */
        public MainTitleBuilder title(String title) {
            builder.put("title", title);
            return this;
        }

        /**
         * @param desc 标题辅助信息，建议不超过30个字
         */
        public MainTitleBuilder desc(String desc) {
            builder.put("desc", desc);
            return this;
        }
    }

    public static class SourceBuilder extends AbstractMsgBuilder {
        private SourceBuilder() {
        }

        public static SourceBuilder builder() {
            return new SourceBuilder();
        }

        /**
         * @param iconUrl 来源图片的url
         */
        public SourceBuilder iconUrl(String iconUrl) {
            builder.put("icon_url", iconUrl);
            return this;
        }

        /**
         * @param desc 来源图片的描述，建议不超过13个字
         */
        public SourceBuilder desc(String desc) {
            builder.put("desc", desc);
            return this;
        }

        /**
         * @param descColor 来源文字的颜色，目前支持：0(默认) 灰色，1 黑色，2 红色，3 绿色
         */
        public SourceBuilder descColor(Integer descColor) {
            builder.put("desc_color", descColor);
            return this;
        }
    }

    public static class CardImageBuilder extends AbstractMsgBuilder {
        private CardImageBuilder() {
        }

        /**
         * @param url 图片的url
         */
        public static CardImageBuilder builder(String url) {
            CardImageBuilder cardImageBuilder = new CardImageBuilder();
            cardImageBuilder.builder.put("url", url);
            return cardImageBuilder;
        }

        /**
         * @param aspectRatio 图片的宽高比，宽高比要小于2.25，大于1.3，不填该参数默认1.3
         */
        public CardImageBuilder aspectRatio(Float aspectRatio) {
            builder.put("aspect_ratio", aspectRatio);
            return this;
        }
    }

    public static class ImageTextAreaBuilder extends AbstractMsgBuilder {
        private ImageTextAreaBuilder() {
        }

        /**
         * @param imageUrl 左图右文样式的图片url
         */
        public static ImageTextAreaBuilder builder(String imageUrl) {
            ImageTextAreaBuilder imageTextAreaBuilder = new ImageTextAreaBuilder();
            imageTextAreaBuilder.builder.put("image_url", imageUrl);
            return imageTextAreaBuilder;
        }

        /**
         * @param type 左图右文样式区域点击事件，0或不填代表没有点击事件，1 代表跳转url，2 代表跳转小程序
         */
        public ImageTextAreaBuilder type(Integer type) {
            builder.put("type", type);
            return this;
        }

        /**
         * @param url 点击跳转的url，image_text_area.type是1时必填
         */
        public ImageTextAreaBuilder url(String url) {
            builder.put("url", url);
            return this;
        }

        /**
         * @param appId 点击跳转的小程序的appid，必须是与当前应用关联的小程序，image_text_area.type是2时必填
         */
        public ImageTextAreaBuilder appId(String appId) {
            builder.put("appid", appId);
            return this;
        }

        /**
         * @param pagePath 点击跳转的小程序的pagepath，image_text_area.type是2时选填
         */
        public ImageTextAreaBuilder pagePath(String pagePath) {
            builder.put("pagepath", pagePath);
            return this;
        }

        /**
         * @param title 左图右文样式的标题
         */
        public ImageTextAreaBuilder title(String title) {
            builder.put("title", title);
            return this;
        }

        /**
         * @param desc 左图右文样式的描述
         */
        public ImageTextAreaBuilder desc(String desc) {
            builder.put("desc", desc);
            return this;
        }
    }

    public static class VerticalContentBuilder extends AbstractMsgBuilder {
        private VerticalContentBuilder() {
        }

        /**
         * @param title 卡片二级标题，建议不超过26个字
         */
        public static VerticalContentBuilder builder(String title) {
            VerticalContentBuilder verticalContentBuilder = new VerticalContentBuilder();
            verticalContentBuilder.builder.put("title", title);
            return verticalContentBuilder;
        }

        /**
         * @param desc 二级普通文本，建议不超过112个字
         */
        public VerticalContentBuilder desc(String desc) {
            builder.put("desc", desc);
            return this;
        }
    }

    public static class EmphasisContentBuilder extends AbstractMsgBuilder {
        private EmphasisContentBuilder() {
        }

        public static EmphasisContentBuilder builder() {
            return new EmphasisContentBuilder();
        }

        /**
         * @param title 关键数据样式的数据内容，建议不超过10个字
         */
        public EmphasisContentBuilder title(String title) {
            builder.put("title", title);
            return this;
        }

        /**
         * @param desc 关键数据样式的数据描述内容，建议不超过15个字
         */
        public EmphasisContentBuilder desc(String desc) {
            builder.put("desc", desc);
            return this;
        }
    }

    public static class QuoteAreaBuilder extends AbstractMsgBuilder {
        private QuoteAreaBuilder() {
        }

        public static QuoteAreaBuilder builder() {
            return new QuoteAreaBuilder();
        }

        /**
         * @param type 引用文献样式区域点击事件，0或不填代表没有点击事件，1 代表跳转url，2 代表跳转小程序
         */
        public QuoteAreaBuilder type(Integer type) {
            builder.put("type", type);
            return this;
        }

        /**
         * @param url 点击跳转的url，quote_area.type是1时必填
         */
        public QuoteAreaBuilder url(String url) {
            builder.put("url", url);
            return this;
        }

        /**
         * @param appId 点击跳转的小程序的appid，quote_area.type是2时必填
         */
        public QuoteAreaBuilder appId(String appId) {
            builder.put("appid", appId);
            return this;
        }

        /**
         * @param pagePath 点击跳转的小程序的pagepath，quote_area.type是2时选填
         */
        public QuoteAreaBuilder pagePath(String pagePath) {
            builder.put("pagepath", pagePath);
            return this;
        }

        /**
         * @param title 引用文献样式的标题
         */
        public QuoteAreaBuilder title(String title) {
            builder.put("title", title);
            return this;
        }

        /**
         * @param quoteText 引用文献样式的引用文案
         */
        public QuoteAreaBuilder quoteText(String quoteText) {
            builder.put("quote_text", quoteText);
            return this;
        }
    }

    public static class HorizontalContentBuilder extends AbstractMsgBuilder {
        private HorizontalContentBuilder() {
        }

        /**
         * @param keyName 二级标题，建议不超过5个字
         */
        public static HorizontalContentBuilder builder(String keyName) {
            HorizontalContentBuilder horizontalContentBuilder = new HorizontalContentBuilder();
            horizontalContentBuilder.builder.put("keyname", keyName);
            return horizontalContentBuilder;
        }

        /**
         * @param type 模版卡片的二级标题信息内容支持的类型，1是url，2是文件附件，3 代表点击跳转成员详情
         */
        public HorizontalContentBuilder type(Integer type) {
            builder.put("type", type);
            return this;
        }

        /**
         * @param value 二级文本，如果horizontal_content_list.type是2，该字段代表文件名称（要包含文件类型），建议不超过26个字
         */
        public HorizontalContentBuilder value(String value) {
            builder.put("value", value);
            return this;
        }

        /**
         * @param url 链接跳转的url，horizontal_content_list.type是1时必填
         */
        public HorizontalContentBuilder url(String url) {
            builder.put("url", url);
            return this;
        }

        /**
         * @param mediaId 附件的media_id，horizontal_content_list.type是2时必填
         */
        public HorizontalContentBuilder mediaId(String mediaId) {
            builder.put("media_id", mediaId);
            return this;
        }

        /**
         * @param userId 成员详情的userid，horizontal_content_list.type是3时必填
         */
        public HorizontalContentBuilder userId(String userId) {
            builder.put("userid", userId);
            return this;
        }
    }

    public static class JumpBuilder extends AbstractMsgBuilder {
        private JumpBuilder() {
        }

        /**
         * @param title 跳转链接样式的文案内容，建议不超过13个字
         */
        public static JumpBuilder builder(String title) {
            JumpBuilder jumpBuilder = new JumpBuilder();
            jumpBuilder.builder.put("title", title);
            return jumpBuilder;
        }

        /**
         * @param type 跳转链接类型，0或不填代表不是链接，1 代表跳转url，2 代表跳转小程序
         */
        public JumpBuilder type(Integer type) {
            builder.put("type", type);
            return this;
        }

        /**
         * @param url 跳转链接的url，jump_list.type是1时必填
         */
        public JumpBuilder url(String url) {
            builder.put("url", url);
            return this;
        }

        /**
         * @param appId 跳转链接的小程序的appid，jump_list.type是2时必填
         */
        public JumpBuilder appId(String appId) {
            builder.put("appid", appId);
            return this;
        }

        /**
         * @param pagePath 跳转链接的小程序的pagepath，jump_list.type是2时选填
         */
        public JumpBuilder pagePath(String pagePath) {
            builder.put("pagepath", pagePath);
            return this;
        }
    }

    public static class CardActionBuilder extends AbstractMsgBuilder {
        private CardActionBuilder() {
        }

        /**
         * @param type 卡片跳转类型，1 代表跳转url，2 代表打开小程序
         */
        public static CardActionBuilder builder(Integer type) {
            CardActionBuilder cardActionBuilder = new CardActionBuilder();
            cardActionBuilder.builder.put("type", type);
            return cardActionBuilder;
        }

        /**
         * @param url 跳转事件的url，card_action.type是1时必填
         */
        public CardActionBuilder url(String url) {
            builder.put("url", url);
            return this;
        }

        /**
         * @param appId 跳转事件的小程序的appid，card_action.type是2时必填
         */
        public CardActionBuilder appId(String appId) {
            builder.put("appid", appId);
            return this;
        }

        /**
         * @param pagePath 跳转事件的小程序的pagepath，card_action.type是2时选填
         */
        public CardActionBuilder pagePath(String pagePath) {
            builder.put("pagepath", pagePath);
            return this;
        }
    }

}
